/**
 * 
 */
package jmine.tec.report.cell.impl;

import java.io.Serializable;
import java.text.NumberFormat;

import jmine.tec.report.cell.FormattedCell;

/**
 * Celula que calcula a média entre os valores.
 * 
 * @author takeshi
 */
public final class MeanFunctionDataCell extends AbstractDoubleFunctionDataCell implements Serializable, FormattedCell {

    /**
     * 
     */
    private static final long serialVersionUID = 1714127840400788988L;

    private final NumberFormat numberFormat;

    /**
     * Default constructor
     */
    public MeanFunctionDataCell() {
        this.numberFormat = null;
    }

    /**
     * @param numberFormat the numberFormat
     */
    public MeanFunctionDataCell(NumberFormat numberFormat) {
        this.numberFormat = numberFormat;
    }

    /**
     * Returns the single instance of this class, lazy loading if necessary
     * 
     * @return the single instance
     */
    public static MeanFunctionDataCell getInstance() {
        return new MeanFunctionDataCell();
    }

    /**
     * @param values the values
     * @return double
     * @see br.com.maps.util.report.cell.impl.AbstractDoubleFunctionDataCell#combine(double[])
     */
    @Override
    protected double combine(final double... values) {
        double sum = 0;
        for (double d : values) {
            sum += d;
        }
        return sum / values.length;
    }

    /**
     * {@inheritDoc}
     */
    public String format() {
        if (this.numberFormat != null) {
            return this.numberFormat.format(this.toDouble().doubleValue());
        } else {
            return this.toDouble().toString();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public NumberFormat getNumberFormat() {
        return this.numberFormat;
    }

}
